<template>
  <a-layout>
    <a-layout-sider width="400" style="background: #fff">
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="12" :sm="24">
                <a-form-item label="班次名称">
                  <a-input v-model="queryParam.MaterialName" placeholder />
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-button type="primary" @click="getDataList">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <a-table
          ref="table"
          :columns="columns"
          :rowKey="row => row.WorkingTimesID"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
          @rowClick="rowClick"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :bordered="true"
          size="small"
        >
        </a-table>

        <edit-form ref="editForm" :afterSubmit="getDataList"></edit-form>
      </a-card>
    </a-layout-sider>
    <a-layout>
      <a-layout-content
        :style="{ margin: '0px 0px 0px 5px', padding: '0px', background: '#fff', minHeight: '280px' }"
      >

        <DetailList ref="DetailList" :key="currentKey" :row-data="rowData"></DetailList>

      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script>
import EditForm from './EditForm'
import DetailList from '@/views/ApsWorkTimes/WorkTimeDetail'
const columns = [
  { title: '班次名称', dataIndex: 'WorkingTimesName', width: '200px' }
]

export default {
  components: {
    EditForm,
    DetailList
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      currentKey: '',
      rowData: {},
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'WorkingTimesID', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      visible: false,
      selectedRowKeys: []
    }
  },
  methods: {
    rowClick (row) {
      this.rowData = row
      this.currentKey = (new Date()).valueOf()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Aps/Aps_WorkingTimes/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'WorkingTimesID',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.submitDelete(ids, resolve, reject)
          }).catch(() => console.log('Oops errors!'))
        }
      })
    },
    submitDelete (ids, resolve, reject) {
      this.$http.post('/Aps/Aps_WorkingTimes/DeleteData', ids).then(resJson => {
        resolve()

        if (resJson.Success) {
          this.$message.success('操作成功!')

          this.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    }
  }
}
</script>
